# coding: utf-8
import json

from . import db


class BarCode(db.Model):
    __tablename__ = 't_bar_code'

    id = db.Column(db.Integer, primary_key=True)
    url_link = db.Column(db.String(200))
    pictrue_path = db.Column(db.String(200))
    device_type = db.Column(db.String(10))
    bar_code = db.Column(db.String(50), unique=True)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceDetail(db.Model):
    __tablename__ = 't_device_detail'
    __table_args__ = (
        db.Index('\u8bbe\u5907\u786c\u4ef6\u552f\u4e00', 'device_id', 'hardware_type'),
    )

    id = db.Column(db.Integer, primary_key=True)
    device_id = db.Column(db.String(20))
    device_type = db.Column(db.String(20))
    hardware_type = db.Column(db.String(50))
    hardware_version = db.Column(db.String(100))
    firmware_version = db.Column(db.String(100))
    app = db.Column(db.String(20))
    mfg = db.Column(db.String(250))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceFault(db.Model):
    __tablename__ = 't_device_fault'

    id = db.Column(db.BigInteger, primary_key=True)
    device_id = db.Column(db.String(20), index=True)
    fault = db.Column(db.String(50))
    fault_desc = db.Column(db.String(200))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class DeviceFaultRepair(db.Model):
    __tablename__ = 't_device_fault_repair'

    id = db.Column(db.Integer, primary_key=True)
    bar_code = db.Column(db.String(20))
    type = db.Column(db.Integer)
    description = db.Column(db.String(200))
    repair = db.Column(db.Integer, server_default=db.FetchedValue())
    repair_desc = db.Column(db.String(200))
    repair_time = db.Column(db.DateTime)
    repair_user_id = db.Column(db.Integer)
    user_id = db.Column(db.BigInteger)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceHardware(db.Model):
    __tablename__ = 't_device_hardware'

    id = db.Column(db.Integer, primary_key=True)
    device_type = db.Column(db.String(10))
    hardware_type = db.Column(db.String(10))
    hardware_version = db.Column(db.String(50))
    firmware_version = db.Column(db.String(50))
    firmware_url_a = db.Column(db.String(200))
    firmware_url_b = db.Column(db.String(200))
    firmware_name_a = db.Column(db.String(100))
    firmware_name_b = db.Column(db.String(100))
    firmware_md5_a = db.Column(db.String(100))
    firmware_md5_b = db.Column(db.String(100))
    firmware_size_a = db.Column(db.Integer)
    firmware_size_b = db.Column(db.Integer)
    status = db.Column(db.Integer, server_default=db.FetchedValue())
    create_time = db.Column(db.DateTime)
    update_time = db.Column(db.DateTime)
    remark = db.Column(db.String(100))
    province_code = db.Column(db.String(10))
    city_code = db.Column(db.String(10))
    area_code = db.Column(db.String(10))


class DeviceHardwareBlacklist(db.Model):
    __tablename__ = 't_device_hardware_blacklist'

    id = db.Column(db.Integer, primary_key=True)
    device_id = db.Column(db.String(50), unique=True)
    auto_update = db.Column(db.Integer, server_default=db.FetchedValue())
    remark = db.Column(db.String(100))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceInfo(db.Model):
    __tablename__ = 't_device_info'

    id = db.Column(db.Integer, primary_key=True)
    device_id = db.Column(db.String(20), unique=True)
    online = db.Column(db.Integer, server_default=db.FetchedValue())
    bar_code = db.Column(db.String(32), unique=True)
    fault = db.Column(db.Integer, server_default=db.FetchedValue())
    device_type_code = db.Column(db.String(10))
    is_used = db.Column(db.Integer, server_default=db.FetchedValue())
    locked = db.Column(db.Integer, server_default=db.FetchedValue())
    province_code = db.Column(db.String(10))
    city_code = db.Column(db.String(10))
    area_code = db.Column(db.String(10))
    store_num = db.Column(db.String(20))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)

    def jsonstr(self):
        return json.dumps(self, cls=AlchemyEncoder)


class DeviceProduction(db.Model):
    __tablename__ = 't_device_production'

    id = db.Column(db.Integer, primary_key=True)
    device_info_id = db.Column(db.BigInteger, nullable=False)
    product_com = db.Column(db.String(50), nullable=False)
    product_line = db.Column(db.String(50))
    period_time = db.Column(db.DateTime)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceTest(db.Model):
    __tablename__ = 't_device_test'

    id = db.Column(db.BigInteger, primary_key=True)
    msgid = db.Column(db.String(20), index=True)
    device_id = db.Column(db.String(40))
    cmd_data = db.Column(db.String(255))
    status = db.Column(db.Integer)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)
    power_send = db.Column(db.Integer)
    power_rec = db.Column(db.Integer)


class DeviceTest20190311(db.Model):
    __tablename__ = 't_device_test_20190311'

    id = db.Column(db.BigInteger, primary_key=True)
    msgid = db.Column(db.String(20), index=True)
    device_id = db.Column(db.String(40))
    cmd_data = db.Column(db.String(255))
    status = db.Column(db.Integer)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)
    power_send = db.Column(db.Integer)
    power_rec = db.Column(db.Integer)


class DeviceTest20190328(db.Model):
    __tablename__ = 't_device_test_20190328'

    id = db.Column(db.BigInteger, primary_key=True)
    msgid = db.Column(db.String(20), index=True)
    device_id = db.Column(db.String(40))
    cmd_data = db.Column(db.String(255))
    status = db.Column(db.Integer)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)
    power_send = db.Column(db.Integer)
    power_rec = db.Column(db.Integer)


class DeviceTest20190401(db.Model):
    __tablename__ = 't_device_test_20190401'

    id = db.Column(db.BigInteger, primary_key=True)
    msgid = db.Column(db.String(20), index=True)
    device_id = db.Column(db.String(40))
    cmd_data = db.Column(db.String(255))
    status = db.Column(db.Integer)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)
    power_send = db.Column(db.Integer)
    power_rec = db.Column(db.Integer)


class DeviceTestBeforeOnline(db.Model):
    __tablename__ = 't_device_test_before_online'

    id = db.Column(db.BigInteger, primary_key=True)
    bar_code = db.Column(db.String(20), unique=True)
    device_id = db.Column(db.String(20), unique=True)


class DeviceType(db.Model):
    __tablename__ = 't_device_type'

    id = db.Column(db.Integer, primary_key=True)
    device_type_code = db.Column(db.String(10), nullable=False, unique=True, server_default=db.FetchedValue())
    name = db.Column(db.String(50))
    img_url = db.Column(db.String(255))
    net_type = db.Column(db.Integer)
    dev_length = db.Column(db.Integer)
    dev_width = db.Column(db.Integer)
    dev_high = db.Column(db.Integer)
    dev_weight = db.Column(db.Integer)
    dev_power = db.Column(db.Integer)
    description = db.Column(db.String(200))
    remark = db.Column(db.String(200))
    transport_size = db.Column(db.String(60))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceWarn(db.Model):
    __tablename__ = 't_device_warn'

    id = db.Column(db.BigInteger, primary_key=True)
    bar_code = db.Column(db.String(20))
    device_id = db.Column(db.String(20), nullable=False)
    device_type = db.Column(db.String(20), nullable=False)
    temp = db.Column(db.Integer, nullable=False)
    warn_temp = db.Column(db.Integer)
    type = db.Column(db.Integer, nullable=False)
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceWarnAll(db.Model):
    __tablename__ = 't_device_warn_all'

    id = db.Column(db.BigInteger, primary_key=True)
    bar_code = db.Column(db.String(20))
    device_id = db.Column(db.String(20), nullable=False)
    device_type = db.Column(db.String(20), nullable=False)
    temp = db.Column(db.Integer)
    warn_temp = db.Column(db.Integer)
    shutdown_temp = db.Column(db.Integer)
    remark = db.Column(db.String(255))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class DeviceWatcher(db.Model):
    __tablename__ = 't_device_watcher'

    id = db.Column(db.BigInteger, primary_key=True)
    bar_code = db.Column(db.String(20), unique=True)
    device_id = db.Column(db.String(20))
    device_type = db.Column(db.String(20))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class MacInfo(db.Model):
    __tablename__ = 't_mac_info'

    id = db.Column(db.Integer, primary_key=True)
    mac = db.Column(db.String(20), unique=True)
    pwd = db.Column(db.String(100))
    version = db.Column(db.String(50))
    name = db.Column(db.String(50))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class MitvVideo(db.Model):
    __tablename__ = 't_mitv_video'

    id = db.Column(db.BigInteger, primary_key=True)
    picture_url = db.Column(db.String(255))
    file_url = db.Column(db.String(255))
    file_name = db.Column(db.String(255))
    sort = db.Column(db.Integer, nullable=False)
    carousel = db.Column(db.String(1))
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)


class UserDevice(db.Model):
    __tablename__ = 't_user_device'
    __table_args__ = (
        db.Index('user_device', 'user_id', 'device_id'),
    )

    id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.BigInteger)
    device_id = db.Column(db.String(20))
    device_type = db.Column(db.String(10))
    deivce_name = db.Column(db.String(255))
    device_img_url = db.Column(db.String(255))
    owner_user_id = db.Column(db.BigInteger)
    idx = db.Column(db.Integer, server_default=db.FetchedValue())
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime)
